package com.app.micaihu.view.utils;

import android.view.View;
import android.view.ViewGroup;

import com.blankj.utilcode.util.ScreenUtils;

public class ViewUtils {
    /**
     * 根据输入的宽和高按比例更改图片的宽和高
     *
     * @param view
     */
    public static void changeViewScale(View view, int portion, int widthScale, int heightScale, int margin) {
        ViewGroup.LayoutParams params = view.getLayoutParams();
        int screenWidth = ScreenUtils.getScreenWidth();
        int width;
        if (portion > 0) {
            width = screenWidth / portion;
        } else {
            width = screenWidth;
        }
        if (margin > 0) {
            width = width - margin;
        }
        params.width = width;
        params.height = width * heightScale / widthScale;
        view.requestLayout();
        view.invalidate();
    }

    public static void changeViewWidth(View view, int width) {
        ViewGroup.LayoutParams params = view.getLayoutParams();
        params.width = width;
        view.requestLayout();
        view.invalidate();
    }

    public static void changeViewHeight(View view, int height) {
        ViewGroup.LayoutParams params = view.getLayoutParams();
        params.height = height;
        view.requestLayout();
        view.invalidate();
    }

    public static void changeViewWidthAndHeight(View view, int width) {
        ViewGroup.LayoutParams params = view.getLayoutParams();
        params.width = width;
        params.height = width;
        view.requestLayout();
        view.invalidate();
    }
}
